﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TopicList : System.Web.UI.Page
{
    Topic topic = new Topic();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Write("<script language=javascript>alert('请先登录！')</script>");
            Response.Redirect("Login.aspx");
        }
        else
        {
            DataSet ds = topic.GetTopicList();
            gvTopicList.DataSource = ds;
            gvTopicList.DataBind();
            lblPages.Text = "当前页数 "+Convert.ToString(gvTopicList.PageIndex+1)+" 共"+gvTopicList.PageCount.ToString()+" 页";
        }
    }
    /// <summary>
    /// 按钮列单击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvTopicList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int iIndex = Convert.ToInt32(e.CommandArgument);//获取命令参数，包括行索引等
        int iTopicID=-1;
        string strUserName = gvTopicList.Rows[iIndex].Cells[1].Text;
        switch (e.CommandName)
        { 
            case "Update":
                
                if (strUserName == "Guest" || strUserName != Session["UserName"].ToString())
                {
                    Response.Write("<script language=javascript>alert('您无权修改!')</script>");
                }
                else
                {
                    iTopicID = Convert.ToInt32(gvTopicList.Rows[iIndex].Cells[0].Text);
                    Response.Redirect("ModifyTopic.aspx?TopicID=" + iTopicID);
                }
                break;
            case "Delete":
                
                if (strUserName == "Guest" || strUserName != Session["UserName"].ToString())
                {
                    Response.Write("<script language=javascript>alert('您无权删除!')</script>");
                }
                else
                {
                    iTopicID = Convert.ToInt32(gvTopicList.Rows[iIndex].Cells[0].Text);
                    bool bIsSuccess = topic.DeleteTopic(iTopicID);
                    if (bIsSuccess == true)
                    {
                        Response.Write("<script language=javascript>alert('删除成功!')</script>");
                        Page_Load(sender,e);
                    }
                }
                break;
        }
    }
    protected void gvTopicList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
    }
    protected void gvTopicList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        
    }
    protected void gvTopicList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        lblPages.Text = "当前页数 " + Convert.ToString(gvTopicList.PageIndex + 1) + " 共" + gvTopicList.PageCount.ToString() + " 页";
    }
}
